<template>
  <div class="formDiv">
    <el-form ref="form" :model="register" label-width="170px" :rules="rules">
      <el-form-item label="单位登记所在地：" :rules="[{required: true}]">
        <el-radio-group v-model.trim="register.applyType">
          <el-radio v-for="applyType in applyTypes" :label="applyType.dictValue"
                :key="applyType.dictValue">{{applyType.dictLabel}}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="register.applyType == 0 || register.applyType == 1" label="法人类型：" prop="legalPersonType">
        <el-select v-model.trim="register.legalPersonType" style="width: 100%" placeholder="请选择法人类型">
          <el-option v-for="legalpersonType in legalpersonTypes" :value="legalpersonType.dictValue"
                  :key="legalpersonType.dictValue" :label="legalpersonType.dictLabel"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="register.applyType == 0 || register.applyType == 1" label="企业名称：" prop="companyName">
        <el-input v-model.trim="register.companyName" placeholder="请输入企业名称" maxLength="50"></el-input>
      </el-form-item>
      <el-form-item v-if="register.applyType == 0 || register.applyType == 1" label="统一社会信用代码：" prop="companyCode">
        <el-input v-model.trim="register.companyCode" placeholder="请输入统一社会信用代码" maxLength="18" @input="register.companyCode = register.companyCode.replace(/[^\w]/g,'')"></el-input>
      </el-form-item>
      <el-form-item v-if="register.applyType == 0 || register.applyType == 1" label="法定代表人：" prop="companyPerson">
        <el-input v-model.trim="register.companyPerson" maxLength="20" placeholder="请输入法定代表人"></el-input>
      </el-form-item>
      <el-form-item v-if="register.applyType == 0 || register.applyType == 1" label="法定代表人证件类型：" prop="companyCertificatesType">
        <el-select v-model.trim="register.companyCertificatesType" style="width: 100%" placeholder="请选择代表人证件类型">
          <el-option v-for="companyCertificatesType in legalRepresentativeTypes"
                  :value="companyCertificatesType.dictValue"
                  :key="companyCertificatesType.dictValue"
                  :label="companyCertificatesType.dictLabel"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="register.applyType == 0 || register.applyType == 1" label="法定代表人证件号码：" prop="companyCertificatesNumber">
        <el-input v-model.trim="register.companyCertificatesNumber" placeholder="请输入法定代表人证件号码" maxLength="30" @input="register.companyCertificatesNumber = register.companyCertificatesNumber.replace(/[^\w]/g,'')"></el-input>
      </el-form-item>

      <el-form-item v-if="register.applyType == 0 || register.applyType == 1" prop="isRead">
        <el-checkbox v-model.trim="register.isRead">
        </el-checkbox>
        <span style="margin-left: 10px">我已阅读并同意</span>
        <span style="color: #045db9" class="agreementDiv">《裕华区政府企业服务用户注册协议》</span>
      </el-form-item>

      <el-form-item>
        <div class="btnDiv">
          <Buttons @toAuth="toAuth" @backPage="backPage" @submit="submit" :buttons="buttons" class="btns"></Buttons>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { getFormOptions, getCompanyAuthentication } from "@/common/api/logOrReg.js";

  export default {
    props: {
      value: {
        type: Number,
        default: 1
      }
    },
    computed:{
      buttons(){
        let btns = [{text:"返回",type:"normal",size:"l",method:"backPage"}];
        if(this.register.applyType == 0 || this.register.applyType == 1){
          btns.push({text:"信息认证",type:"success",size:"l",method:"submit"})
        }else{
          btns.push({text:"人工认证",type:"success",size:"l",method:"toAuth"})
        }
        return btns
      }
    },
    data() {
      return {
        legalRepresentativeTypes: [],
        legalpersonTypes: [],
        applyTypes: [
					{dictLabel: "裕华区内", dictValue: "0"},
					{dictLabel: "非裕华区", dictValue: "1"},
					{dictLabel: "非中华人民共和国大陆地区", dictValue: "2"},
					{dictLabel: "非法人单位", dictValue: "3"}
				],
        register: {
					applyType: "0",
					legalPersonType: null,
					companyName: '',
					companyCode: '',
					companyPerson: '',
					companyCertificatesType: null,
					companyCertificatesNumber: '',
					isRead: false
				},
        rules: {
          legalPersonType: [
            { required: true, trigger: 'blur', message: '请选择法人类型' }
          ],
          companyName: [
            { required: true, trigger: 'blur', message: '请输入企业名称' }
          ],
          companyCode: [
            { required: true, trigger: 'blur', message: '请输入统一社会信用代码' }
          ],
          companyPerson: [
            { required: true, trigger: 'blur', message: '请输入法定代表人' }
          ],
          companyCertificatesType: [
            { required: true, trigger: 'blur', message: '请选择代表人证件类型' }
          ],
          companyCertificatesNumber: [
            { required: true, trigger: 'blur', message: '请输入法定代表人证件号码' }
          ],
          isRead: [
            { required: true, trigger: 'blur', message: '请阅读并同意协议后再认证' },
            { trigger:'blur', validator: (rule, value, callback) => {
                if(!value) {
                  callback(new Error('请阅读并同意协议后再认证'))
                } else {
                  callback()
                }
              }
            }
          ]
        }
      }
    },
    mounted() {
			let ns = this
			ns.getFormOptions()
      this.register.applyType = "0";
		},
    activated(){
      this.register.applyType = "0";
    },
    methods: {
      /**
			 * 获取选项信息
			 */
			async getFormOptions() {
        let res = await getFormOptions({})
			  this.legalRepresentativeTypes = res.data.legal_representative_type,
				this.legalpersonTypes = res.data.legalperson_type
			},
      backPage() {
				window.history.go(-1)
			},
      /**
			 * 认证企业信息
			 */
      submit() {
				this.$refs['form'].validate((valid) => {
					if(valid) {
            this.infoAuth()
					}
				})
      },
			async infoAuth() {
        let res = await getCompanyAuthentication(this.register)
        if(res.code == 200) {
          // 缓存当前表单信息
          localStorage.setItem('preForm', JSON.stringify(this.register))
          localStorage.setItem('clueSource', "1")
          this.next()
        } else {
          this.$confirm('无法找到该企业，是否进入人工认证渠道？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          }).then(() => {
            this.toAuth()
          })
        }
			},
      toAuth() {
        localStorage.setItem('clueSource', "0")
				let ns = this
				ns.$router.push({path: 'menAuthentication', query: {type: ns.register.applyType}})
			},
      prev() {
        this.$emit('input', this.value-1)
      },
      next() {
        this.$emit('input', this.value+1)
      }
    }
  }
</script>

<style lang="scss" scoped>
.formDiv {
  margin: 0 auto 100px auto;
  width: 750px;
  text-align: left;
  .agreementDiv:hover {
    cursor: pointer;
    text-decoration: underline;
  }
  .btnDiv {
    display: flex;
    .btn {
      background-color: #f1f2f4;
      color: #045db9;
      margin-right: 20px;
      width: 100px;
      text-align: center;
      font-size: 15px;
      border-radius: 5px;
      &:hover {
        cursor: pointer;
        box-shadow: 0px 0px 5px #888888;
        transition: all 0.2s ease 0s;
      }
    }
  }
  #codeDiv {
    display: flex;
    .sendCodeBtn {
      white-space: nowrap;
      background-color: #f1f2f4;
      color: #045db9;
      width: 120px;
      text-align: center;
      border-top-right-radius: 4px;
      border-bottom-right-radius: 4px;
      &:hover {
        cursor: pointer;
        box-shadow: 0px 0px 3px #888888;
        transition: all 0.2s ease 0s;
      }
    }
    .cantSendBtn {
      white-space: nowrap;
      background-color: #f1f2f4;
      color: #045db9;
      width: 120px;
      text-align: center;
      border-top-right-radius: 4px;
      border-bottom-right-radius: 4px;
      &:hover {
        cursor: pointer;
      }
    }
  }
}
::v-deep .formDiv #codeDiv .el-input .el-input__inner {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}
</style>